/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.setting.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.common.core.util.StringUtils;
import com.hopes.bizz.risk.setting.entity.RiskSafetyWarningLabelEntity;
import com.hopes.bizz.risk.setting.service.RiskSafetyWarningLabelService;
import com.hopes.bizz.risk.setting.vo.RiskSafetyWarningLabelExcelVO;
import com.hopes.boss.admin.api.feign.RemoteFileService;
import com.hopes.boss.common.core.constant.SecurityConstants;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.data.tenant.TenantContextHolder;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * 安全警示标示
 *
 * @author liujiaqi
 * @date 2023-07-24 10:30:35
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskSafetyWarningLabel" )
@Tag(description = "riskSafetyWarningLabel" , name = "安全警示标示管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskSafetyWarningLabelController {

    private final  RiskSafetyWarningLabelService riskSafetyWarningLabelService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param riskSafetyWarningLabel 安全警示标示
     * @return
     */
    @SuppressWarnings("unchecked")
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('risk_safety_warning_label_view')" )
    public R getRiskSafetyWarningLabelPage(Page page, RiskSafetyWarningLabelEntity riskSafetyWarningLabel) {
        LambdaQueryWrapper<RiskSafetyWarningLabelEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(riskSafetyWarningLabel.getType()),RiskSafetyWarningLabelEntity::getType,riskSafetyWarningLabel.getType());
		wrapper.like(StrUtil.isNotBlank(riskSafetyWarningLabel.getLabelName()),RiskSafetyWarningLabelEntity::getLabelName,riskSafetyWarningLabel.getLabelName());
		wrapper.orderByAsc(RiskSafetyWarningLabelEntity::getType, RiskSafetyWarningLabelEntity::getLabelName);
        return R.ok(riskSafetyWarningLabelService.page(page, wrapper));
    }
    @Operation(summary = "页面展示" , description = "页面展示" )
    @GetMapping("/list" )
    @PreAuthorize("@pms.hasPermission('risk_safety_warning_label_view')" )
    public R getRiskSafetyWarningLabelList( RiskSafetyWarningLabelEntity riskSafetyWarningLabel) {
        LambdaQueryWrapper<RiskSafetyWarningLabelEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Objects.nonNull(riskSafetyWarningLabel.getType()),RiskSafetyWarningLabelEntity::getType,riskSafetyWarningLabel.getType());
        wrapper.eq(StrUtil.isNotBlank(riskSafetyWarningLabel.getLabelName()),RiskSafetyWarningLabelEntity::getLabelName,riskSafetyWarningLabel.getLabelName());
        return R.ok(riskSafetyWarningLabelService.list(wrapper));
    }




    /**
     * 通过id查询安全警示标示
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('risk_safety_warning_label_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(riskSafetyWarningLabelService.getById(id));
    }

    /**
     * 新增安全警示标示
     * @param riskSafetyWarningLabel 安全警示标示
     * @return R
     */
    @Operation(summary = "新增安全警示标示" , description = "新增安全警示标示" )
    @SysLog("新增安全警示标示" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_safety_warning_label_add')" )
    public R save(@RequestBody RiskSafetyWarningLabelEntity riskSafetyWarningLabel) {
        return R.ok(riskSafetyWarningLabelService.save(riskSafetyWarningLabel));
    }

    /**
     * 修改安全警示标示
     * @param riskSafetyWarningLabel 安全警示标示
     * @return R
     */
    @Operation(summary = "修改安全警示标示" , description = "修改安全警示标示" )
    @SysLog("修改安全警示标示" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('risk_safety_warning_label_edit')" )
    public R updateById(@RequestBody RiskSafetyWarningLabelEntity riskSafetyWarningLabel) {
        return R.ok(riskSafetyWarningLabelService.updateById(riskSafetyWarningLabel));
    }

    /**
     * 通过id删除安全警示标示
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除安全警示标示" , description = "通过id删除安全警示标示" )
    @SysLog("通过id删除安全警示标示" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('risk_safety_warning_label_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(riskSafetyWarningLabelService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param riskSafetyWarningLabel 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('risk_safety_warning_label_export')" )
    public void export(RiskSafetyWarningLabelEntity riskSafetyWarningLabel, HttpServletResponse response) {
        try (final ServletOutputStream outputStream = response.getOutputStream()){
            riskSafetyWarningLabelService.export(riskSafetyWarningLabel, outputStream);
        }catch (Exception e) {
            log.error("导出安全警示信息异常", e);
        }
    }


}
